package com.clean.cleantotal.compores.start

import android.animation.ValueAnimator
import android.os.Bundle
import android.view.animation.DecelerateInterpolator
import androidx.core.animation.doOnEnd
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.SPUtils
import com.clean.cleantotal.MainActivity
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.compores.battery.BatteryActivity
import com.clean.cleantotal.compores.delete.DeleteActivity
import com.clean.cleantotal.compores.device.DeviceActivity
import com.clean.cleantotal.compores.process.ProcessActivity
import com.clean.cleantotal.databinding.ActivityStartBinding
import com.clean.notification.noti.RequestNotiPerUtils

class StartActivity : BaseActivity<ActivityStartBinding>() {
    override val viewBinding: ActivityStartBinding
        get() = ActivityStartBinding.inflate(layoutInflater)

    private var goToFun = ""
    override fun initParams(extras: Bundle?) {
        super.initParams(extras)
        extras?.let {
            goToFun = it.getString("from", "")
        }
    }

    override fun initView() {
        RequestNotiPerUtils.requestNotify(this, lifecycleScope) {
            val animator = ValueAnimator.ofInt(0, 100)
            animator.duration = 3000
            animator.addUpdateListener {
                mBinding.progress.progress = it.animatedValue as Int
            }
            animator.doOnEnd {
                val isFirst = SPUtils.getInstance().getBoolean("isFirst", true)
                if (isFirst) {
                    openActivity(FirstBatteryActivity::class.java)
                } else {
                    openActivity(MainActivity::class.java)
                    when (goToFun) {
                        "clean" -> {
                            openActivity(DeleteActivity::class.java, Bundle().apply {
                                putSerializable("funType", FunType.CLEAN)
                                putString("title", "Clean")
                            })
                        }

                        "process" -> {
                            openActivity(ProcessActivity::class.java)
                        }

                        "battery" -> {
                            openActivity(BatteryActivity::class.java)
                        }

                        "device" -> {
                            openActivity(DeviceActivity::class.java)
                        }
                    }
                }
                finish()
            }
            animator.interpolator = DecelerateInterpolator()
            animator.start()
        }
    }

    override fun onBackPressed() {

    }
}